var Results = {
    
    /**
     * Initializes results interface
     * 
     */
    init: function() {
        this.configUI();
    },
    
    /**
     * Sets up UI for results interface
     */
    configUI: function() {
        this.configRevertModal();
        this.configTransRows();
    },

    
    /**
     * Sets up UI elements for the Transactions display on the right
     */
    configTransRows: function() {
        $(".credit_authorize").click(function(e){
            e.preventDefault();
            var url = $(this).attr('href');
            var amount = $(this).attr('data-amount');
            var student = $(this).attr('data-student');
            var r=window.confirm("You are about to authorize a credit amount of GHS "+amount+". to "+student+". Are you sure you want to continue ?.");
            if (!r){
                return false;
            }
            window.location = url;
        });

        $("#message_modal_close_callback").click(function(e){
            window.location.reload(false)
        });
        
        $(".revert_btn").live('click', function(e) {
            e.preventDefault();
            var tr = $(this).parents('tr');
            
            $("#trans_reversal_modal form").get(0).reset();
            $("#trans_reversal_modal").find('#pay_method').html( tr.attr("data-payment_method") );
            $("#trans_reversal_modal").find('#amount').html( tr.attr("data-amount") );
            $("#trans_reversal_modal").find('#transaction_debit_amt').val( tr.attr("data-amount") );
            $("#trans_reversal_modal").find("#transaction_id").val( tr.attr("data-trans-id") );
            $("#trans_reversal_modal").find("#student_id").val( tr.attr("data-stu-id") );
            
            $("#trans_reversal_modal").modal('show');
            setTimeout(function() {
                $("#trans_reversal_modal textarea").focus();
            }, 500);
        });
    },

    /**
     * sets up UI for teh revert transaction modal
     * @returns {undefined}
     */
    configRevertModal: function() {
        $("#trans_reversal_modal form").submit(function(e) {
            e.preventDefault();
            Results.revertTransaction();
        });
    },


    /**
     * Enables/Disables the form elements on the Credit Account form
     * 
     * @param boolean disable
     * @returns void
     */
    disableCreditForm: function(disable) {
        if( disable ) {
            $("#credit_modal form :input").attr("disabled","disabled");
            $("#credit_modal #credit_btn").html("<i class='icon icon-spinner'></i> Processing").addClass("disabled");
        } 
        
        else {
            $("#credit_modal form :input").removeAttr("disabled");
            $("#credit_modal #credit_btn").html("Credit Account").removeClass("disabled");
            $("#credit_modal form").get(0).reset();
            $(".cheque_row").hide();
        }
    },
    
    /**
     * Triggers the AJAX call to make a transaction as requiring reversal
     */
    revertTransaction: function() {
        if( !confirm("You are about to revert this transaction and potentially refund the amount paid.\n\nClick OK to continue or CANCEL to return to previous screen")) {
            return;
        }
        
        var query = $("#trans_reversal_modal form").serialize();
        var url = $("#trans_reversal_modal form").attr("action");
        
        $.ajax({
            type: 'post',
            url: url,
            data: query,
            dataType: 'json',
            success: function(data) {
                $("#trans_reversal_modal").modal("hide");
                if(data.status == 'success'){
                    Results.showMessage("Revert Transaction", data.message);
                }
                else{
                    Results.showMessage("Failed To Revert Transaction", data.message);
                }
            },
            error: function(xhr) {
                console.log(xhr.responseText);
            }
        });
    },
    

    /**
     * Displays an alert message
     * 
     * @param {type} title
     * @param {type} message
     * @param {type} type
     * @returns {undefined}
     */
    showMessage: function(title, message, type) {
        $("#message_modal .modal-header h4").html(title);
        $("#message_modal .modal-body").html(message);
        
        $("#message_modal").modal('show');
    }
};

$(document).ready(function() {
    Results.init();
});
